﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OUSMS.AppLogic.DataAccessLayer;
namespace OUSMS.AppLogic.BusinessLogicLayer
{
    [System.ComponentModel.DataObject(true)]
    public class StudyAreasBLL : IDisposable
    {
        DatabaseContext _DatabaseContext;
        public StudyAreasBLL():this(new DatabaseContext())
        { 
        }

        public StudyAreasBLL(DatabaseContext DatabaseContext)
        {
            _DatabaseContext = DatabaseContext;
        }

        public void Dispose()
        {
            if (_DatabaseContext != null)
            {
                _DatabaseContext.Dispose();
                _DatabaseContext = null;
            }

            GC.SuppressFinalize(this);
        }

        public IQueryable<StudyAreas> GetALLStuyAreas()
        {
            return _DatabaseContext.StudyAreas.Where(s => !s.IsDeleted);
        }

        public IQueryable<StudyAreas> GetAllActiveStudyAreas(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            IQueryable<StudyAreas> studyAreas = _DatabaseContext.StudyAreas.Where(d => !d.IsDeleted);
            totalRowCount = studyAreas.Count();
            return studyAreas.OrderBy(d => d.Name).Skip(startRowIndex).Take(maximumRows);
        }

        public void AddStudyArea(StudyAreas studyArea)
        {
            _DatabaseContext.StudyAreas.Add(studyArea);
            _DatabaseContext.SaveChanges();
        }

        public StudyAreas GetStudyAreaByID(int id)
        {
            return _DatabaseContext.StudyAreas.Where(s => s.ID == id).FirstOrDefault();
        }

        public void SaveChanges()
        {
            _DatabaseContext.SaveChanges();
        }
    }
}